package com.zhoug.ocr.textin.parser;

import android.util.Log;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.zhoug.common.utils.GsonUtils;
import com.zhoug.common.utils.JsonUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;
import com.zhoug.ocr.textin.data.FormResult;
import com.zhoug.ocr.textin.data.HeaderField;

import java.util.ArrayList;
import java.util.List;

/**
 * 学校晨检记录台账数据json格式化
 *
 * @Author: zhoug
 * @Date: 2023/3/2
 * @Description:
 */
public class MorningCheckParser extends BaseFormResultParser {
    private DefaultFormResultParser mDefaultParser;
    private List<HeaderField> startHeader;
    private List<HeaderField> endHeader;
    private List<String> hygiene;
    private List<String> health;
    private List<HeaderField> projectHeaders;


    public MorningCheckParser() {
        this(getDefaultStartHeaders(), getDefaultEndHeaders(), getDefaultHygiene(), getDefaultHealth());
    }

    public MorningCheckParser(List<HeaderField> startHeader, List<HeaderField> endHeader, List<String> hygiene, List<String> health) {
        mDefaultParser = new DefaultFormResultParser();
        if (startHeader != null && startHeader.size() > 0) {
            this.startHeader = startHeader;
        } else {
            this.startHeader = getDefaultStartHeaders();
        }
        this.endHeader = endHeader;
        if (hygiene != null && hygiene.size() > 0) {
            this.hygiene = hygiene;
        } else {
            this.hygiene = getDefaultHygiene();
        }
        if (health != null && health.size() > 0) {
            this.health = health;
        } else {
            this.health = getDefaultHealth();
        }
        projectHeaders = new ArrayList<>();
        addProjectHeader(this.hygiene, 1);
        addProjectHeader(this.health, 2);
    }

    public static List<HeaderField> getDefaultStartHeaders() {
        List<HeaderField> startHeader = new ArrayList<>();
        startHeader.add(new HeaderField("检查日期", "MorningCheckTime"));
        startHeader.add(new HeaderField("姓名", "FirmUserName"));
        startHeader.add(new HeaderField("岗位", "Jobs"));
        return startHeader;
    }

    public static List<HeaderField> getDefaultEndHeaders() {
        List<HeaderField> endHeader = new ArrayList<>();
        endHeader.add(new HeaderField("处理措施", "MeasureName"));
        endHeader.add(new HeaderField("检查人", "CheckPeople"));
        return endHeader;
    }

    public static List<String> getDefaultHygiene() {
        List<String> hygiene = new ArrayList<>();
        hygiene.add("戴首饰");
        hygiene.add("留长指甲");
        hygiene.add("衣帽不整洁");
        return hygiene;
    }

    public static List<String> getDefaultHealth() {
        List<String> health = new ArrayList<>();
        health.add("发热");
        health.add("外伤");
        health.add("腹泻");
        health.add("咽炎");
        health.add("重感冒");
        return health;
    }

    private void addProjectHeader(List<String> names, int type) {
        for (int i = 0; i < names.size(); i++) {
            projectHeaders.add(new HeaderField(names.get(i), "pro" + i + "0_" + type));
            projectHeaders.add(new HeaderField(names.get(i), "pro" + i + "1_" + type));
        }
        projectHeaders.add(new HeaderField("其它", "proOther" + "_" + type));
    }


    @Override
    public String parseFormResult(FormResult formResult, List<HeaderField> headerFields, boolean byHeaderOrder) {
        if (DEBUG) {
            LogUtils.d(TAG, "parseFormResult:formResult=" + formResult);
        }
        headerFields = new ArrayList<>();
        headerFields.addAll(this.startHeader);
        FormResult formResult1 = filterEmptyRowCol(formResult);
        List<HeaderField> headers = parseHeader(formResult1, headerFields);
        headers = parseHeaderByOrder(headers, headerFields);
        JsonArray jsonArray = mDefaultParser.parseLine(formResult, headers);
        if (jsonArray != null && jsonArray.size() > 0) {
//            List<MorningCheck> checks = new ArrayList<>();
            JsonArray checks=new JsonArray();
            for (int i = 0; i < jsonArray.size(); i++) {
                JsonObject jsonObject = (JsonObject) jsonArray.get(i);
                if (DEBUG) {
                    LogUtils.d(TAG, "parseFormResult:jsonObject=" + jsonObject.toString());
                }
                JsonObject check= (JsonObject) new JsonParser().parse(jsonObject.toString());
                checks.add(check);
                JsonArray items = new JsonArray();
                check.add("items",items);
                for (int j = 0; j < projectHeaders.size(); ) {
                    HeaderField projectHeaderY = projectHeaders.get(j);
                    if (projectHeaderY.getField().contains("proOther")) {
                        JsonObject item = new JsonObject();
                        items.add(item);
//                        item.setCheckItemName(projectHeaderY.getName());
//                        item.setCheckItemType(Integer.parseInt(projectHeaderY.getField().split("_")[1]));
//                        item.setResultsDesc(GsonUtils.getAsString(jsonObject, projectHeaderY.getField()));
                        item.addProperty("CheckItemType",Integer.parseInt(projectHeaderY.getField().split("_")[1]));
                        item.addProperty("CheckItemName",projectHeaderY.getName());
                        String resultsDesc = GsonUtils.getAsString(jsonObject, projectHeaderY.getField());
                        item.addProperty("ResultsDesc",resultsDesc);
                        if(StringUtils.isNullOrEmpty(resultsDesc)){
                            item.addProperty("IsAbnormal",false);
                        }else{
                            item.addProperty("IsAbnormal",true);
                        }
                        j += 1;
                    } else {
                        HeaderField projectHeaderN = projectHeaders.get(j + 1);
                        String value1 = GsonUtils.getAsString(jsonObject, projectHeaderY.getField());
                        String value2 = GsonUtils.getAsString(jsonObject, projectHeaderN.getField());
                        JsonObject item = new JsonObject();
                        items.add(item);
//                        item.setCheckItemName(projectHeaderY.getName());
//                        item.setCheckItemType(Integer.parseInt(projectHeaderY.getField().split("_")[1]));
                        item.addProperty("CheckItemName",projectHeaderY.getName());
                        item.addProperty("CheckItemType",Integer.parseInt(projectHeaderY.getField().split("_")[1]));
                        if (!StringUtils.isNullOrEmpty(value1)) {
//                            item.setResult(true);
//                            item.setResultsDesc("是");
                            item.addProperty("IsAbnormal",true);
                            item.addProperty("ResultsDesc","是");
                        } else if (!StringUtils.isNullOrEmpty(value2)) {
//                            item.setResult(false);
//                            item.setResultsDesc("否");
                            item.addProperty("IsAbnormal",false);
                            item.addProperty("ResultsDesc","否");
                        }else{
                            item.addProperty("IsAbnormal",false);
                        }
                        j += 2;
                    }

                }
            }
            return checks.toString();
        }
        return null;
    }

    @Override
    public List<HeaderField> parseHeaderByOrder(List<HeaderField> headers, List<HeaderField> headerFields) {
        if (headerFields == null || headerFields.size() == 0) return null;
        if (headers != null && headers.size() > 0) {
            int headerIndex = 0;
            HeaderField headerField = null;
            out:
            for (int i = 0; i < headerFields.size(); i++) {
                HeaderField field1 = headerFields.get(i);
                for (HeaderField field2 : headers) {
                    if (field1.getField().equals(field2.getField())) {
                        headerField = field2;
                        headerIndex = i;
                        break out;
                    }
                }
            }
            headerFields.addAll(this.projectHeaders);
            if (this.endHeader != null && this.endHeader.size() > 0) {
                headerFields.addAll(this.endHeader);
            }

            int startRow = headerField.getStartRow();
            int endRow = headerField.getEndRow();
            int startCol = headerField.getStartCol();
            int endCol = headerField.getEndCol();
            for (int i = 0; i < headerFields.size(); i++) {
                HeaderField headerField1 = headerFields.get(i);
                headerField1.setStartRow(startRow);
                headerField1.setEndRow(endRow);
                if (i == headerIndex) {
                    headerField1.setStartCol(startCol);
                    headerField1.setEndCol(endCol);
                } else if (i < headerIndex) {
                    headerField1.setStartCol(startCol - (headerIndex - i));
                    headerField1.setEndCol(endCol - (headerIndex - i));
                } else {
                    headerField1.setStartCol(startCol + (i - headerIndex));
                    headerField1.setEndCol(endCol + (i - headerIndex));
                }
            }
        }
        if (DEBUG) {
            Log.d(TAG, "parseHeaderByOrder:headerFields=" + JsonUtils.toJson(headerFields));
        }
        return headerFields;
    }

}
